package com.witmore.skyline.web.exception;

import com.witmore.skyline.web.constants.WeChatConstant;
import org.apache.commons.codec.binary.Hex;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.http.ResponseEntity;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.security.MessageDigest;
import java.util.Arrays;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;

@Service
public class AsyncWeChatNotifier {

    @Autowired
    private Environment environment;

    private String getEnvironmentProperty() {
        boolean prod = Arrays.asList(environment.getActiveProfiles()).contains("prod");
        if (prod) {
            return "【生产环境】\n";
        }
        boolean dev = Arrays.asList(environment.getActiveProfiles()).contains("dev");
        if (dev) {
            return "【开发环境】\n";
        }
        return "【异常环境】\n";
    }

    public void sendAIToWeChatAsync(String message, String ossUrl) throws Exception {
        String hookUrl = String.format(WeChatConstant.WECHAT_WEBHOOK_END_POINT, WeChatConstant.WECHAT_WEBHOOK_AI_KEY);
        // 2. 发送图片消息
        this.sendImageToWeChatAsync(ossUrl, hookUrl);
        Thread.sleep(200);
        // 1. 发送文本消息
        this.sendTextToWeChatAsync(message, hookUrl);
    }

    @Async("threadPoolExecutor")
    public void sendSystemErrorToWeChatAsync(String message) {
        String hookUrl = String.format(WeChatConstant.WECHAT_WEBHOOK_END_POINT, WeChatConstant.WECHAT_WEBHOOK_SERVER_ERROR_KEY);
        this.sendTextToWeChatAsync(message, hookUrl);
    }


    private void sendTextToWeChatAsync(String message, String hookUrl) {
        message = getEnvironmentProperty() + message;
        RestTemplate restTemplate = new RestTemplate();

        Map<String, Object> requestBody = new HashMap<>();
        requestBody.put("msgtype", "text");
        Map<String, String> textContent = new HashMap<>();
        textContent.put("content", message);
        requestBody.put("text", textContent);
        restTemplate.postForObject(hookUrl, requestBody, String.class);
    }


    private void sendImageToWeChatAsync(String ossUrl, String hookUrl) throws Exception {
        RestTemplate restTemplate = new RestTemplate();
        // 2. 下载 OSS 图片并发送
        ResponseEntity<byte[]> response = restTemplate.getForEntity(ossUrl, byte[].class);
        byte[] imageBytes = response.getBody();

        // 3. 转换为 base64 和计算 MD5
        String base64Image = Base64.getEncoder().encodeToString(imageBytes);
        MessageDigest md = MessageDigest.getInstance("MD5");
        String md5 = Hex.encodeHexString(md.digest(imageBytes));

        // 4. 构建图片消息请求体
        Map<String, Object> imageMsg = new HashMap<>();
        imageMsg.put("msgtype", "image");

        Map<String, String> imageContent = new HashMap<>();
        imageContent.put("base64", base64Image);
        imageContent.put("md5", md5);
        imageMsg.put("image", imageContent);

        // 5. 发送图片消息
        restTemplate.postForObject(hookUrl, imageMsg, String.class);
    }
}